package com.yisu.auction.api.web.controller;

import com.yisu.auction.api.core.service.IArchiveFileService;
import com.yisu.auction.api.domain.request.ArchiveFileRequest;
import com.yisu.auction.api.domain.request.BasePage;
import com.yisu.auction.api.web.interceptor.IAuthVerifyAnnotation;
import com.yisu.auction.ewf.common.enums.LoginTypeEnum;
import com.yisu.auction.ewf.common.enums.ResponseCode;
import com.yisu.auction.ewf.common.web.JsonResult;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * author: 赵杰
 * Business Description：归档业务
 * createTime：2017/9/11
 */
@Controller
@RequestMapping(value = "/archiveFile")
public class ArchiveFileController extends BaseController {
    @Resource
    private IArchiveFileService iArchiveFileService;

    /**
     * author: 赵杰
     * Business Description：竞品归档业务操作
     *
     * @param
     * @return 系统参数
     */
    @RequestMapping(value = "/archivePrize", method = {RequestMethod.POST,RequestMethod.GET}, produces = "application/json; charset=utf-8")
    @ResponseBody
    @IAuthVerifyAnnotation(Desc = "登录验证", LoginTypeEnum = LoginTypeEnum.NO_NEED_LOGIN)
    public JsonResult archivePrize() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Calendar date = Calendar.getInstance();
        date.setTime(new Date());
        date.add(Calendar.DATE, -1);
        final String archiveFileTime = simpleDateFormat.format(date.getTime()) + " 00:00:00";
        final Integer archivePrizeCount = iArchiveFileService.getArchivePrizeCountByArchiveTime(archiveFileTime);
        if (archivePrizeCount > 0) {
            Integer totalPages = archivePrizeCount / 10 + 1;
            for (int i = 1; i <= totalPages; i++) {
                ExecutorService executorService = Executors.newFixedThreadPool(1);
                final int finalI = i;
                Runnable syncRunnable = new Runnable() {
                    @Override
                    public void run() {
                        ArchiveFileRequest archiveFileRequest = new ArchiveFileRequest();
                        archiveFileRequest.setArchiveFileTime(archiveFileTime);
                        archiveFileRequest.setPageindex(finalI);
                        archiveFileRequest.setPagesize(20);
                        iArchiveFileService.archivePrizeData(archiveFileRequest);
                    }
                };
                executorService.execute(syncRunnable);
            }
        }
        return new JsonResult(ResponseCode.SUCCESS.code, "归档成功！");

      /*  SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Calendar date = Calendar.getInstance();
        date.setTime(new Date());
        date.add(Calendar.DATE, -1);
        final String archiveFileTime = simpleDateFormat.format(date.getTime()) + " 00:00:00";
        final Integer archivePrizeCount = iArchiveFileService.getArchivePrizeCountByArchiveTime(archiveFileTime);
        if (archivePrizeCount > 0) {
            ExecutorService executorService = Executors.newFixedThreadPool(1);
            Runnable syncRunnable = new Runnable() {
                @Override
                public void run() {
                    Integer totalPages = archivePrizeCount / 20 + 1;
                    for (int i = 1; i <= totalPages; i++) {
                        ArchiveFileRequest archiveFileRequest = new ArchiveFileRequest();
                        archiveFileRequest.setArchiveFileTime(archiveFileTime);
                        archiveFileRequest.setPageindex(i);
                        archiveFileRequest.setPagesize(50);
                        iArchiveFileService.archivePrizeData(archiveFileRequest);
                    }
                }
            };
            executorService.execute(syncRunnable);
        }
        return new JsonResult(ResponseCode.SUCCESS.code, "归档成功！");*/
    }
}
